﻿/***************************************************************************
//* Foxit Software Inc.
//* Copyright(C) 2005-2012, all rights reserved.
//*
//* The following code is copyrighted and contains proprietary information
//* and trade secrets of Foxit Software Inc.                                                      *
***************************************************************************/

/**
* PDF页面显示
*/

define(function (require, exports, module) {
    require('../ViewLevel.js');
    var CPDF_Page = require('../PDFData/PDFPage.js');
    var CPDF_Rect = require('../Math/Rect');
    var CPDF_Point = require('../Math/Point');
    var Config = require("../Config");
    /**
     * PDF Page view
     * @class PDF Page view
     * @constructor
     * @param {FXREADER.Data.CPDF_Page} pPDFPage 	 PDF page object 
     * @param {FXREADER.PDFView.CPDF_DocView} pDocView doc view object
 	* @param {String} docViewContainerID  ID of the doc view div.
     * @return a page view
     */
    /**
     * @param pPDFPage
     * @param pDocView
     * @param docViewContainerID
     * @returns {FXREADER.PDFView.CPDF_PageView}
     */
    FXREADER.PDFView.CPDF_PageView = function (pPDFPage, pDocView, docViewContainerID) {
        if (pPDFPage == null)
            throw new Error("pPDFPage is null!");
        var _pPDFPage = pPDFPage,
              _pDocView = pDocView,
              _Scale = pDocView.GetScale();
        var _PageViewWidth = 0, _PageViewHeight = 0, _bContentCreated = false;
        //页面大小是否更新过,当改变放大倍数时重置;
        var _bScaleUpdated = false;
        var _bPageLoaded = false; //页面是否已经Load完成
        var _bPageLoadError = false; //页面加载失败，返回背景图片为失败提示图片   hjp,Add
        var nPageIndex = _pPDFPage.GetPageIndex();
        var _PageContainerID = _pDocView.GetPageViewContainerID(nPageIndex);
        var _PageBGImgID = _pDocView.GetPageBGImgID(nPageIndex); //背景图
        var _WrapPageID = _pDocView.GetWrapPageID(nPageIndex);
        var _PageLoaderImgID = _pDocView.GetPageLoaderImgID(nPageIndex);
        var _self = this;

        
        this.IsThumb = function () {
            return false;
        };
        /**
         * @description Get the PDF page object
         * @return {FXREADER.Data.CPDF_Page} the PDF page object
         */
        this.GetPDFPage = function () {
            return _pPDFPage;
        };
        /**
         * @description Get the width of PDF page 
         * @return {Number} the width of PDF page 
         */
        this.GetPDFPageWidth = function () {
            return _pPDFPage.GetPageWidth();
        };
        /**
         * @description Get the height of PDF page 
         * @return {Number} the height of PDF page 
         */
        this.GetPDFPageHeight = function () {
            return _pPDFPage.GetPageHeight();
        };
        /**
         * @description Get the width of PDF page view
         * @return {Number} the width of PDF page view
         */
        this.GetPageViewWidth = function () {
            return _PageViewWidth;
        };
        this.SetPageViewWidth = function (width) {
            if (isNaN(width))
                return;
            _PageViewWidth = width;
        };
        /**
         * @description Get the height of PDF page view
         * @return {Number} the height of PDF page view
         */
        this.GetPageViewHeight = function () {
            return _PageViewHeight;
        };
        this.SetPageViewHeight = function (height) {
            if (isNaN(height))
                return;
            _PageViewHeight = height;
        };
        /**
         * @description Get the page index of the page
 		* @return {Number} the page index of the page 
         */
        this.GetPageIndex = function () {
            return _pPDFPage.GetPageIndex();
        };

        this.GetPageContainerID = function () {
            return _PageContainerID;
        };

        this.GetPageBGImgID = function () {
            return _PageBGImgID;
        };

        this.GetWrapPageID = function () {
            return _WrapPageID;
        };
        this.GetPageLoaderImgID = function () {
            return _PageLoaderImgID;
        };
        /**
         * @description Get the doc view object
         * @return {FXREADER.PDFView.CPDF_DocView} the doc view object
         */
        this.GetDocView = function () {
            return _pDocView;
        };

        //设置page对应的div是否创建的状态;
        this.SetContentCreatedState = function (bCreated) {
            _bContentCreated = bCreated;
        };
        this.IsContentCreated = function () {
            return _bContentCreated;
        };

        this.IsScaleUpdated = function () {
            return _bScaleUpdated;
        };
        this.SetScaleUpdated = function (bUpdated) {
            _bScaleUpdated = bUpdated;
        };
        /**
         * @description whether the page is a thumbnail.
         * @return {Boolean} return true if the page is a thumbnail, otherwise return false.
         */
        this.IsThumb = function () {
            return false;
        };

        this.UpdateDimension = function () {
            var nPageViewWidth = _self.GetPDFPageWidth() * _pDocView.GetScale() * _pDocView.GetPageViewRender().GetPixelsPerPoint();
            var nPageViewHeight = _self.GetPDFPageHeight() * _pDocView.GetScale() * _pDocView.GetPageViewRender().GetPixelsPerPoint();
            _self.SetPageViewWidth(nPageViewWidth);
            _self.SetPageViewHeight(nPageViewHeight);
            var scrollApi = _pDocView.GetScrollApi();//.find(".jspVerticalBar");
            var scrollBarWidth = 0;
            if(scrollApi!=null && scrollApi.getIsScrollableV())
            {
            	scrollBarWidth = scrollApi.getContentPane().parent().find(".jspVerticalBar").width();
            }
            
            
            var nLeftMargin = (_pDocView.GetDocViewWidth() - nPageViewWidth - scrollBarWidth ) / 2;
            if (nLeftMargin < 0)
                nLeftMargin = 0;
            $("#" + _self.GetPageContainerID()).css({//这边改变了页面大小
                height: nPageViewHeight,
                width: nPageViewWidth,
                "margin-left": nLeftMargin
            });
        };

        //显示页面
        this.Show = function () {
            $("#" + _WrapPageID).addClass("fwr_hidden");
            $("#" + _PageBGImgID).removeClass("fwr_hidden");
            $("#" + _PageLoaderImgID).hide();
        };

        this.ShowErrorPage = function (imgurl) {
            var jqWrap = $("#" + _WrapPageID);
            jqWrap.html('');
            jqWrap.removeClass("fwr_hidden");
            $("#" + _PageBGImgID).addClass("fwr_hidden");
            $("#" + _PageLoaderImgID).hide();

            var fZoomX = Config.errPageImgWidth / _PageViewWidth;
            var fZoomY = Config.errPageImgHeight / _PageViewHeight;
            var fZoom = fZoomX >= fZoomY ? fZoomX : fZoomY;
            fZoom = fZoom > 1 ? fZoom : 1;
            var fRealWidth = Config.errPageImgWidth / fZoom;
            var fRealHeight = Config.errPageImgHeight / fZoom;
            var marginLeft = (_PageViewWidth - fRealWidth) / 2;
            var marginTop = (_PageViewHeight - fRealHeight) / 2;

            var html = "<div style='position:absolute;width:" + fRealWidth + "px;height:" + fRealHeight + "px;margin-left:" + marginLeft + "px;margin-top:" + marginTop + "px'><img alt='' src='" + imgurl + "'  style='width:100%;height:100%'></div>";
            jqWrap.append(html);
        };
        /**
         * @description check whether the page is loaded.
         * @return {Boolean} return true if the page is loaded, otherwise return false.
         */
        this.IsPageLoaded = function () {
            return _bPageLoaded;
        };
        this.SetPageLoaded = function (bLoad) {
            _bPageLoaded = bLoad;
        };
        /**
         * @description check whether there is an error when load the page.
         * @return {Boolean} return true if there is an error when load the page, otherwise return false.
         */
        this.IsPageLoadError = function () {
            return _bPageLoadError;
        };
        this.SetPageLoadError = function (bLoadError) {
            _bPageLoadError = bLoadError;
        };

        this.GetScale = function () {
            return _Scale;
        };

        this.SetScale = function (scale) {
            _Scale = scale;
        };

        /**
         * @description transform from PDF space(user space) to device space
         * @param {FXREADER.CPDF_Rect} rect rectangle to transform
         * @param bScale whether to multi by zoom level
         */
        this.PDF2Doc = function (rect, bScale) {
            FXREADER.RectUtils.Normalize(rect);
            var nOldWidth = FXREADER.RectUtils.Width(rect);
            var nOldHeight = FXREADER.RectUtils.Height(rect);
            var newRect = new CPDF_Rect(0, 0, 0, 0);
            var scale = _self.GetScale();
            var nPixelsPerPt = _pDocView.GetPageViewRender().GetPixelsPerPoint();
            newRect.left = (rect.left * nPixelsPerPt) * scale;
            if (bScale)
                newRect.right = newRect.left + nOldWidth * nPixelsPerPt * scale;
            else
                newRect.right = newRect.left + nOldWidth * nPixelsPerPt;

            newRect.top = (rect.top) * nPixelsPerPt * scale;
            newRect.top = _PageViewHeight - newRect.top;
            if (bScale)
                newRect.bottom = newRect.top + nOldHeight * nPixelsPerPt * scale;
            else
                newRect.bottom = newRect.top + nOldHeight * nPixelsPerPt;
            return newRect;
        };
        /**
         * @description transform from device space to PDF space(user space)
         * @param {FXREADER.CPDF_Point} docPt point to transform
         */
        this.DocPt2PDF = function (docPt) {
            var scale = _self.GetScale();
            var nPixelsPerPt = _pDocView.GetPageViewRender().GetPixelsPerPoint();
            var pdfPt = new CPDF_Point(0, 0);
            pdfPt.x = docPt.x / (nPixelsPerPt * scale);
            pdfPt.y = (_PageViewHeight - docPt.y) / (nPixelsPerPt * scale);
            return pdfPt;
        };

        //获取页面div相对于父层div的偏移量 格式 {left:xx,top:xx}
        this.GetPageViewDivOffset = function () {
            var e = $("#" + _self.GetPageContainerID());
            var eleTop = 0, eleLeft = 0;
            eleTop += e.get(0).offsetTop;
            eleLeft += e.get(0).offsetLeft;
            return { left: eleLeft, top: eleTop };
        };
        
        this.GetPageViewRect = function()
        {
        	var offset = _self.GetPageViewDivOffset();
        	return new FXREADER.CPDF_Rect(offset.left,offset.top,offset.left+_self.GetPageViewWidth()
        			,offset.top+_self.GetPageViewHeight());
        };
        
        /**
         * 根据位置和类型取annot，其中类型可以为空
         * */
        this.GetAnnotAtPoint = function(event, annotType)
        {
        	return null;
        };
        
        this.GetFocusAnnot = function()
        {
        	return null;
        };
        
        this.SetFocusAnnot = function(annot)
        {
        	
        };
    };

    return FXREADER.PDFView.CPDF_PageView;
});